package com.gwcm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gwcm.exception.BusinessException;
import com.gwcm.model.dto.memberCard.MemberCardAddRequest;
import com.gwcm.model.dto.memberCard.MemberCardDeleteRequest;
import com.gwcm.model.dto.memberCard.MemberCardQueryRequest;
import com.gwcm.model.dto.memberCard.MemberCardUpdateRequest;
import com.gwcm.model.entity.MemberCard;
import com.gwcm.model.vo.MemberCardVo;
import com.gwcm.service.MemberCardService;
import com.yhz.commonutil.common.BaseResponse;
import com.yhz.commonutil.common.ErrorCode;
import com.yhz.commonutil.common.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/memberCard")
@Api(tags = "会员卡接口")
public class MemberCardController {
    @Autowired
    public MemberCardService memberCardService;

    @ApiOperation("新增会员卡")
    @PostMapping("/save")
    public BaseResponse addMemberCard(@RequestBody MemberCardAddRequest memberCardAddRequest) {
        MemberCard memberCard = new MemberCard();
        BeanUtils.copyProperties(memberCardAddRequest, memberCard);

        memberCardService.saveMemberCard(memberCard);

        return ResultUtils.success();
    }

    @ApiOperation("分页查询会员卡")
    @PostMapping("/page")
    public BaseResponse<IPage<MemberCardVo>> pageModuleTopic(@RequestBody MemberCardQueryRequest memberCardQueryRequest) throws BusinessException {
        IPage<MemberCardVo> memberCardVoIPage = memberCardService.pageList(memberCardQueryRequest);
        return ResultUtils.success(memberCardVoIPage);
    }

    @ApiOperation("修改会员卡")
    @PostMapping("/update")
    public BaseResponse updateMemberCard(@RequestBody MemberCardUpdateRequest memberCardUpdateRequest) {
        MemberCard memberCard = new MemberCard();
        BeanUtils.copyProperties(memberCardUpdateRequest, memberCard);

        memberCardService.updateMemberCard(memberCard);

        return ResultUtils.success();
    }

    @ApiOperation("删除会员卡")
    @PostMapping("/delete")
    public BaseResponse deleteMemberCard(@RequestBody MemberCardDeleteRequest memberCardDeleteRequest) {
        MemberCard memberCard = new MemberCard();
        BeanUtils.copyProperties(memberCardDeleteRequest, memberCard);

        boolean delete = memberCardService.removeById(memberCard);
        if (!delete) {
            throw new BusinessException(ErrorCode.DELETE_ERROR);
        }
        return ResultUtils.success();
    }

    @ApiOperation("选择会员卡套餐")
    @PostMapping("/select")
    public BaseResponse selectMemberCard(@RequestBody MemberCardQueryRequest memberCardQueryRequest){
        return ResultUtils.success(memberCardService.getMemberCardByCardNameVaildMonth(memberCardQueryRequest));
    }
}
